<template>
  <div class="serach-result">
    <!-- 导航栏 -->
    <van-nav-bar
      title="搜索结果"
      left-arrow
      fixed
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <!-- 文章列表 -->
    <van-list
      class="article-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
     >
      <van-cell
        v-for="article in list"
        :key="article.art_id.toString()"
        :title="article.title"
        @click="$router.push('/article/'+article.art_id.toString())"
      />
    </van-list>
    <!-- /文章列表 -->
  </div>
</template>

<script>
import { getSearchResult } from '@/api/search.js'
export default {
  name: 'SearchResult',
  data () {
    return {
      list: [],
      page: 1,
      loading: false,
      finished: false
    }
  },

  methods: {
    async onLoad () {
      const keyword = this.$route.query.keyword
      // 1. 发请求
      const res = await getSearchResult(keyword, this.page)
      this.page++
      const arr = res.data.data.results
      console.log(res)
      // 2. 把数据添加到list中
      this.list.push(...arr)
      // 3. 手动修改加载状态
      this.loading = false
      // 4. 判断是否所有数据加载完成
      if (arr.length === 0) {
        this.finished = true
      }
    }
  }
}
</script>

<style lang="less" scoped>
// 覆盖组件内部的样式 /deep/
/deep/ .van-nav-bar .van-icon {
  color: #fff;
}
.search-result {
  height: 100%;
  overflow: auto;
  .article-list {
    margin-top: 39px;
  }
}
</style>
